//
//  FPFilmcriticViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPFilmcriticViewController.h"
#import "FPFilmcriticView.h"
#import "FPFilmHeaderModel.h"
#import "FPObserverModel.h"
#import "FPFilmModel.h"
#import "FPCriticCell.h"
@interface FPFilmcriticViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)FPFilmcriticView *FilmView;
/**评论者模型*/
@property(nonatomic,strong)NSMutableArray *listArray;
@property(nonatomic,strong)NSMutableArray *filmArray;
@end

@implementation FPFilmcriticViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.frame = CGRectMake(kScreenWidth * 3, 0, kScreenWidth, kScreenHeight);
  //  self.view.backgroundColor = [UIColor purpleColor];
    [self creatTableView];
    [self loadHeaderData];
    [self analysisListData];
}
- (void)creatTableView
{ UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - kImageH(110)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.FilmView = [[FPFilmcriticView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kImageH(150))];
    self.tableView.tableHeaderView = _FilmView;
    [self.view addSubview:_tableView];
}

#pragma mark -----------解析数据
- (void)loadHeaderData
{
    [LORequestManger GET:kHeaderURL success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *reviewDic = dic[@"review"];
              FPFilmHeaderModel *filmHeaderModel = [FPFilmHeaderModel fpFilHeaderModelWith:reviewDic];
        [self.FilmView viewSetValue:filmHeaderModel];
       
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求数据失败");
    }];

}
#pragma mark----------加载列表数据
- (NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}
- (NSMutableArray *)filmArray
{
    if (!_filmArray) {
        _filmArray = [[NSMutableArray alloc]init];
    }
    return _filmArray;
}
- (void)analysisListData
{
    [LORequestManger GET:@"http://api.m.mtime.cn/MobileMovie/Review.api?needTop=false" success:^(id response) {
        NSArray *array = (NSArray *)response;
        for (NSDictionary *dic in array)
        {
            FPObserverModel *OBModel = [FPObserverModel fpObserverModelWith:dic];
            OBModel.IDStr = dic[@"id"];
            NSDictionary *ObjDic = dic[@"relatedObj"];
            FPFilmModel *filmModel = [FPFilmModel fpFilmModelWithDictionary:ObjDic];
            [self.filmArray addObject:filmModel];
            [self.listArray addObject:OBModel];
        }
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求数据失败");
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"cell";
    [tableView registerClass:[FPCriticCell class] forCellReuseIdentifier:indentifier];
    FPCriticCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FPObserverModel *OBModel = self.listArray[indexPath.row];

    FPFilmModel *fmodel = self.filmArray[indexPath.row];
    [cell adjustSubviewsWithContent:fmodel model:OBModel];
  
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
           return kImageH(110);
  }


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , 20)];
    statusBarView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:statusBarView];
    [self.tabBarController.tabBar setHidden:NO];
    
}

@end
